#include "../thread_pool.h"
#include "../user.h"
#include "mysql.h"
/*-------------------------delimiter--------------------*/
void delete_file(user_info*user,char *pwd,char *filename){
    //从filetable中通过user的id和pwd确定文件名
    connect_mysql(user);
    //初始化user
    const char* sql = "DELETE FROM virtual_file_table WHERE owner_id = ? and pwd = ? and filename =?\0";

    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 3) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit (1);
    }
    //设置参数
    int user_id = 0;
    unsigned long user_id_len = 0;
    char user_pwd[100] = {0};
    unsigned long pwd_len = 0;
    char file[100] = {0};
    unsigned long file_len = 0;

    //绑定参数
    MYSQL_BIND bind[3];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &user_id;
    bind[0].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[0].length = &user_id_len;//整型数据不需要指定长度缓冲区

    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer = user_pwd;
    bind[1].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[1].length = &pwd_len;//整型数据不需要指定长度缓冲区

    bind[2].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[2].buffer = file;
    bind[2].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[2].length = &file_len;//整型数据不需要指定长度缓冲区
    //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    user_id = user->user_id;
    user_id_len = 4;
    strcpy(user_pwd,pwd);
    pwd_len = strlen(user_pwd);
    strcpy(file,filename);
    file_len = strlen(file);
    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    } else {
        printf("QUERY OK, %ld row affected.\n",
               mysql_stmt_affected_rows(user->stmt));
    }
    //释放资源
    mysql_stmt_free_result(user->stmt);
    mysql_close(user->pconn);

}
/*------------------delimiter-----------------*/
void delete_file_rmdir(user_info * user,char *pwd,char *filename){
    //先遍历目录下所有文件，若是普通文件直接删除，若是目录文件就递归后删除
    //从filetable中通过user的id和pwd确定文件名
    connect_mysql(user);
    //初始化user
    //拼接pwd和filename得到目标目录
    char Target_dir[100] = {0};
    snprintf(Target_dir,sizeof(Target_dir),"%s/%s",pwd,filename);
    const char* sql = "SELECT filename,type FROM virtual_file_table WHERE owner_id = ? and pwd = ? \0";

    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 2) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit (1);
    }
    //设置参数
    int user_id = 0;
    unsigned long user_id_len = 0;
    char user_pwd[100] = {0};
    unsigned long pwd_len = 0;

    //绑定参数
    MYSQL_BIND bind[2];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &user_id;
    bind[0].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[0].length = &user_id_len;//整型数据不需要指定长度缓冲区

    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer = user_pwd;
    bind[1].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[1].length = &pwd_len;//整型数据不需要指定长度缓冲区

    //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    user_id = user->user_id;
    user_id_len = 4;
    strcpy(user_pwd,Target_dir);
    pwd_len = strlen(user_pwd);
    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //设置输出参数
    char res_filename[100] = {0};
    char res_type[10] = {0};
    MYSQL_BIND res_bind[2];
    memset(res_bind, 0, sizeof(res_bind));
    //绑定输出参数

    res_bind[0].buffer_type = MYSQL_TYPE_VAR_STRING;
    res_bind[0].buffer = res_filename;
    res_bind[0].buffer_length = sizeof(res_filename);//确定值
                                                     //执行绑定操作
    res_bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    res_bind[1].buffer = res_type;
    res_bind[1].buffer_length = sizeof(res_type);//确定值
    ret = mysql_stmt_bind_result(user->stmt, res_bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //再获取数据信息
    ret = mysql_stmt_store_result(user->stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //读取输出的数据
    while (1) {
        ret = mysql_stmt_fetch(user->stmt);
        if (ret == 1 || ret == MYSQL_NO_DATA) {
            break;
        }
        //判断其文件类型
        if(!strcmp(res_type,"r")){
            //如果是普通文件
            delete_file(user,Target_dir,res_filename);//递归删除中，要删除的文件可能是userpwd目录下更深层目录
        }else if(!strcmp(res_type,"d")){
            //如果是目录文件
            delete_file_rmdir(user,Target_dir,res_filename);
            //最后删除自身
        }
    }
    delete_file(user,pwd,filename);//递归删除中，要删除的文件可能是userpwd目录下更深层目录
    //释放资源
    mysql_stmt_free_result(user->stmt);
    mysql_close(user->pconn);

}

